terraform {
  required_providers {
    maas = {
      source  = "registry.terraform.io/maas/maas"
      version = "~>1.0"
    }
  }
}

provider "maas" {
  api_version = "2.0"
  api_key = "L6a6RjZLPtBD5YUQ4N:B5gWnkVym2GWghGE9w:FnCpPUbxazj4jFajU9uY9bMSfuHEp4XH"
  api_url = "http://10.24.35.191:5240/MAAS"
}

# terraform {
#   required_version      = "~> 0.13"  
#   backend "oss" {
#     bucket              = "deeproute-terraform-resources"
#     key                 = "test/services/maas"
#     tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
#     tablestore_table    = "terraform_statelock_table"    
#     region              = "cn-shenzhen"
#   }
# }

# resource "maas_space" "tf_space" {
#   name = "tf-space"
# }

# resource "maas_fabric" "tf_fabric" {
#   name = "tf-fabric"
# }

# resource "maas_vlan" "tf_vlan" {
#   fabric = maas_fabric.tf_fabric.id
#   vid = 14
#   name = "tf-vlan14"
#   space = maas_space.tf_space.name
# }

# resource "maas_subnet" "tf_subnet" {
#   cidr = "10.3.8.0/22"
#   fabric = maas_fabric.tf_fabric.id
#   vlan = maas_vlan.tf_vlan.vid
#   name = "tf_subnet"
#   gateway_ip = "10.3.11.254"
#   dns_servers = [
#     "10.10.10.10",
#   ]
# }

# resource "maas_subnet" "tf_subnet" {
#   cidr = "10.88.88.0/24"
#   fabric = maas_fabric.tf_fabric.id
#   vlan = maas_vlan.tf_vlan.vid
#   name = "tf_subnet"
#   gateway_ip = "10.88.88.1"
#   dns_servers = [
#     "1.1.1.1",
#   ]
#   ip_ranges {
#     type = "reserved"
#     start_ip = "10.88.88.1"
#     end_ip = "10.88.88.50"
#   }
#   ip_ranges {
#     type = "dynamic"
#     start_ip = "10.88.88.200"
#     end_ip = "10.88.88.254"
#   }
# }

# resource "maas_machine" "deeproute_bm1" {
#   hostname = "10-3-8-63"
#   domain = "deeproute.ai"
#   power_type = "openbmc"
#   power_parameters = {
#     power_address = "10.253.253.143"
#     power_user = "USERID"
#     power_pass = "Flzx3kc@lx"
#   }
#   pxe_mac_address = "04:3F:72:BD:86:53"
# }


resource "maas_vm_host_machine" "kvm" {
  vm_host = "local"
  hostname = "tf-test"
  cores = 1
  memory = 2048

  storage_disks {
    size_gigabytes = 15
  }
  # network_interfaces {
  #   name = "eth0"
  #   ip_address = "172.18.0.5"
  # }   
}

# resource "maas_network_interface_link" "virsh_vm1_nic1" {
#   machine = maas_vm_host_machine.kvm.id
#   network_interface = "ens4"
#   subnet = "172.18.0.0/16"
#   mode = "STATIC"
#   ip_address = "172.18.0.9"
#   default_gateway = true
# }

# resource "maas_block_device" "vda" {
#   machine = maas_vm_host_machine.kvm.id
#   name = "vda"
#   id_path = "/dev/vda"
#   is_boot_device = true
#   size_gigabytes = 15
#   tags = [
#     "ssd",
#   ]

#   partitions {
#     size_gigabytes = 10
#     fs_type = "ext4"
#     mount_point = "/"
#   }

#   partitions {
#     size_gigabytes = 4
#     fs_type = "ext4"
#     mount_point = "/data"
#   }
# }

# resource "maas_instance" "kvm" {
#   allocate_params {
#     hostname = maas_vm_host_machine.kvm.hostname
#   }
#   deploy_params {
#     distro_series = "ubuntu-2004-kube-v1.23.16-amd64-20G-2023-02-14-1676354987"
#   }
#   # network_interfaces {
#   #   name = "eth0"
#   #   ip_address = "172.18.192.5"
#   # }  
# }